package utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

import bean.CollectBear;


/**
 * Created by Administrator on 2016/7/27.
 */
public class CollectDB {

    final String Tab = "  DB=====";
    /**
     * 数据库名字
     */
    public static final String DB_NAME = "Car_Collext";
    /**
     * 数据库的版本
     */
    public static final int DB_VERSION = 1;

    private static CollectDB loginDB;

    private SQLiteDatabase db;

    /**
     * 将构造方法私有化
     *
     * @param context
     */
    private CollectDB(Context context) {

        CarServiceOpenHelper dbhelper = new CarServiceOpenHelper(context, DB_NAME, null, DB_VERSION);
        db = dbhelper.getReadableDatabase();
        //
    }

    /**
     * 获取CarServerLoginDB的实例
     *
     * @param context
     * @return
     */
    public synchronized static CollectDB getInstance(Context context) {
        if (loginDB == null) {
            loginDB = new CollectDB(context);
        }
        return loginDB;
    }

    /**
     * 收藏
     *
     * @param collectBear
     */

    public void saveCollect(CollectBear collectBear) {
        List<String> mlist = queryId();
        if(mlist.size()==0){
            ContentValues values = new ContentValues();
            values.put("COL_type", collectBear.getType());
            values.put("COL_id", collectBear.getId());
            db.insert(DB_NAME, null, values);
        }else {
            if(!mlist.contains(collectBear.getId())){
                ContentValues values = new ContentValues();
                values.put("COL_type", collectBear.getType());
                values.put("COL_id", collectBear.getId());
                db.insert(DB_NAME, null, values);
            }
        }

    }

    public List<String> queryId(){
        Cursor cursor = db.rawQuery("select COL_id from Car_Collext", null);
        List<String> userList = new ArrayList<>();
        while (cursor.moveToNext()) {
            String id = cursor.getString(cursor.getColumnIndex("COL_id"));
            userList.add(id);
        }
        return userList;
    }

    /**
     * 根据id删除用户
     */
    public int DeleteCollect(String id) {
        String whereClause = "id" + " = ?";
        String[] whereArgs = {id};
        return db.delete(DB_NAME, null, null);
    }


    /*
  * 查找用户资料二手车
  * */
    public List<CollectBear> getCollectCar() {

        Cursor cursor = db.rawQuery("select * from Car_Collext where COL_type=?", new String[]{"0"});
        List<CollectBear> userList = new ArrayList<>();
        while (cursor.moveToNext()) {
            String id = cursor.getString(cursor.getColumnIndex("COL_id"));
            CollectBear user = new CollectBear();
            user.setId(id);
            userList.add(user);
        }
        return userList;
    }

    /*
 * 查找用户资料二手车维保
 * */
    public List<CollectBear> getCollectCarMain() {

        Cursor cursor = db.rawQuery("select * from Car_Collext where COL_type=?", new String[]{"1"});
        List<CollectBear> userList = new ArrayList<>();
        while (cursor.moveToNext()) {
            String id = cursor.getString(cursor.getColumnIndex("COL_id"));
            CollectBear user = new CollectBear();
            user.setId(id);
            userList.add(user);
        }
        return userList;
    }

    /*
 * 查找发表的帖子
 * */
    public List<CollectBear> getCollectCarPost() {

        Cursor cursor = db.rawQuery("select * from Car_Collext where COL_type=?", new String[]{"2"});
        List<CollectBear> userList = new ArrayList<>();
        while (cursor.moveToNext()) {
            String id = cursor.getString(cursor.getColumnIndex("COL_id"));
            CollectBear user = new CollectBear();
            user.setId(id);
            userList.add(user);
        }
        return userList;
    }

    /**
     * 删除二手车
     */


}









